﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Adams.Web.Data.JummahPrayers
{
    public static class Location
    {
        public static Adams.Web.Models.JummahPrayers.Location.PageLoad PageLoad(string location)
        {
            var data = new Adams.Web.Models.JummahPrayers.Location.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
DECLARE @LocationID int
SELECT @LocationID = LocationID FROM JummahPrayers.Locations WHERE Name = @Location

SELECT LocationID, Name, Street, Street2, City, State, ZipCode, Latitude, Longitude, DisplayOrder, FridayNote, LongNote
FROM JummahPrayers.Locations
WHERE LocationID = @LocationID

SELECT *
FROM JummahPrayers.Prayers
WHERE LocationID = @LocationID
ORDER BY DisplayOrder
", CommandType.Text,
    DB.CreateParameter("@Location", location)))
            {
                if (dr.HasRows)
                {
                    dr.Read();
                    data.Location = DB.InitFromReader<Shared.Entities.JummahPrayers.Location>(dr, new ColMap { NameInDb = "LocationID", NameInObject = "ID" });
                    data.Location.Prayers = new Shared.Entities.JummahPrayers.PrayerCollection();

                    dr.NextResult();
                    while (dr.Read())
                    {
                        var prayer = DB.InitFromReader<Shared.Entities.JummahPrayers.Prayer>(dr, new ColMap { NameInDb = "PrayerID", NameInObject = "ID" });
                        data.Location.Prayers.Add(prayer);
                    }
                }
                return data;
            }
        }
    }
}
